define(function(require,exports,module){
	var ELView=require("elview");
	var Subnav = require("subnav"); 
	var Grid = require("grid");
	var Form =require("form");
	var aw = require("ajaxwrapper");
	var gridFetchProperties ="pkContactAddress," +
		"communityData.pkCommunityData," +
		"communityData.name," +
		"buildingNumber," +
		"unitNumber," +
		"doorNumber," +
		"address.*," +
		"addressStatus.*," +
		"version," +
		"personalInfo.pkPersonalInfo," +
		"personalInfo.name" 
		
	var memberaddresses = ELView.extend({ 
		 initComponent:function(params,widget){
			 var memberaddressgrid=new Grid({
 				parentNode:".J-memberaddressgrid",
 				url:"api/activity/query",
        		params:function() {
    				return{
						"personalInfo.pkPersonalInfo":params.pkPersonalInfo,
						fetchProperties:gridFetchProperties
    				}
    			},
   				model:{
   					head:{
						buttons:[{
							id:"add",
							icon:"icon-plus",
							handler:function(){
								widget.get("memberaddressform").reset();
								widget.get("subnav").setTitle("会员地址信息")
								widget.get("subnav").show(["addressreturn"]).hide(["save","return","contactsreturn"]);
								widget.show([".J-memberaddressform"]).hide([".J-form",".J-contactsgrid",".J-memberaddressgrid"]);
							}
					   }]
					},
   					columns:[{
   						key:"address",
   						name:"会员地址",
   						format:function(value,row){
							if(row.address){
								return (row.address.fullName||"") + (row.communityData.name||"") +"小区"+ (row.buildingNumber||"") +"号楼"+ (row.unitNumber||"") +"单元"+ (row.doorNumber||"")+"门牌号";
							}else{
								return "";
							}
						}
   					},{
   						key:"operate",
   						name:"操作",
   						format:"button",
						formatparams:[{
							key:"edit",
							icon:"icon-edit",
							handler:function(index,data,rowEle){
								var form = widget.get("memberaddressform");
								form.reset();
								form.setData(data);
								widget.get("subnav").show(["addressreturn"]).hide(["save","return","contactsreturn"]);
								widget.show([".J-memberaddressform"]).hide([".J-form",".J-contactsgrid",".J-memberaddressgrid"]);
							}
						},{
							key:"delete",
							icon:"icon-remove",
							handler:function(index,data,rowEle){
								
							}
						}]
   					}]
   				}
 			});
			this.set("memberaddressgrid",memberaddressgrid);
 			
 		 	var memberaddressform=new Form({
	     		parentNode:".J-memberaddressform",
	     		saveaction : function() {
	     			 var data=widget.get("memberaddressform").getData();
	     			 var cardData=widget.get("memberaddressgrid").getData();
	     			 if(data.index !=""){
	     				 cardData[data.index]=data;
	     			 }
	     			 else{
	     				 cardData.push(data);
	     			 }
	     			 widget.get("memberaddressgrid").setData(cardData);
     				 widget.get("subnav").setTitle("会员基本信息");
					 widget.get("subnav").show(["addressreturn"]).hide(["save","return","contactsreturn"]);
					 widget.show([".J-memberaddressform"]).hide([".J-form",".J-contactsgrid",".J-memberaddressgrid"]);
					 widget.get("memberaddressgrid").refresh();
				 },
				 cancelaction:function(){
					 widget.get("subnav").setTitle("会员基本信息");
					 widget.get("subnav").show(["addressreturn"]).hide(["save","return","contactsreturn"]);
					 widget.show([".J-memberaddressform"]).hide([".J-form",".J-contactsgrid",".J-memberaddressgrid"]);
	  			 },
	  			 model:{
					id:"memberaddress",
					items:[{
							name:"pkContactAddress",
							type:"hidden"
						},{
							name:"version",
							defaultValue:"0",
							type:"hidden"
						},{
							name:"address",
							label:"省/市/区（县）",
							type:"place1",
							onItemSelect : function(data,type){
								if(type == "place"){
									widget.get("form").load("communityData",{
										params:{
											"address.code":data.code,
											"status":"Setting",
											fetchProperties:"pkCommunityData,name"
										},
									});
								}
							},
							validate:["required"]
						},{
							name:"communityData",
							label:"小区",
							type:"select",
							url:"api/communitydata/query",
							key:"pkCommunityData",
							value:"name",
							validate:["required"]
						},{
							name:"buildingNumber",
							label:"楼栋号",
							validate:["required"]
						},{
							name:"unitNumber",
							label:"单元号",
							validate:["required"]
						},{
							name:"doorNumber",
							label:"门牌号",
							validate:["required"]
					    }]
				   }
	    	});
	        this.set("memberaddressform",memberaddressform);
		 }
	});
});